<?php

require_once "../../tfsbox_shouts_view.class.php";

class TFsBOX_PHPBB3_View extends TFsBOXShoutsView
	{
		static function renderShoutBoxTitleBar()
		{
			echo "<div id=\"tfsbox_header\">\n";
			echo TFsBOX::Name ." v4 Beta 3 (v". TFsBOX::Version .")\n";
			echo "</div>\n";
		}

		static function renderShout($shout)
			{
				$options = false;
				$sep = ":";
				$r_class = ($shout->report
						 && TFsBOX::$Session->userRanking() != TFsBOXSession::REGULAR)
						 ? " class=\"tfsbox_reported\"" : "";

				$me = "";
				if (strlen($shout->shout) > 3)
				{
					$me = $shout->shout[0] .
						  $shout->shout[1] .
						  $shout->shout[2] .
						  $shout->shout[3];
				}

				if ($me == "/me ")
				{
					$sep = "";
					$shout->shout = str_replace("/me ", "", $shout->shout);
				}

				if (TFsBOX::$Database->isUrlBanned($shout->site))
					$shout->site = TFsBOX_SITE;

				echo "\t<div$r_class";
				if ($me == "/me ")
					echo " style=\"font-weight: bold;\"";
				echo ">\n";

				echo "\t<span class=\"tfsbox_shout_time\">". TFsBOX::$Session->format_date($shout->date) ."</span>\n";

				if ($me == "/me ")
					echo "* ";

				echo "\t<b><a rel=\"nofollow\" href=\"". $shout->site ."\">". $shout->name ."</a>$sep</b>\n";
				echo "\t". generate_text_for_display(self::parseSmilies($shout->shout), 0, 0, (OPTION_FLAG_BBCODE + OPTION_FLAG_LINKS)) ."\n";
				echo "\t\t<div class=\"tfsbox_shout_options\">\n";

				// Banned users need not interact with the system
				if (!TFsBOX::$Database->isIpBanned(TFsBOX::$Session->ip()))
				{
					if (TFsBOX::$Session->userRanking() != TFsBOXSession::REGULAR)
					{
						$options = true;
						$cp = (defined("TFsBOX_CONTROL_PANEL")) ? "/cpanel" : "";

						if ($shout->ranking == TFsBOXSession::REGULAR
						 || TFsBOX::$Session->userRanking() == TFsBOXSession::ADMIN)
						{
							if ($shout->ranking != TFsBOXSession::ADMIN
							 && !TFsBOX::$Database->isIpBanned($shout->ip))
								TFsBOXAbstractView::renderButton("Ban IP", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/ban/ip/".ip2long($shout->ip), TFsBOXSession::urlRoot()."/images/icons/ban-ip.png", " title=\"Shoutbox only\"", 2);

							if (preg_match(TFsBOX::SiteRegex, $shout->site)
							 && $shout->site != TFsBOX_SITE
							 && !TFsBOX::$Database->isUrlBanned($shout->site))
								TFsBOXAbstractView::renderButton("Ban URL", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/ban/url?".rawurlencode($shout->site), TFsBOXSession::urlRoot() ."/images/icons/ban-url.png", NULL, 2);
						}

						TFsBOXAbstractView::renderButton("Edit Shout", "javascript: edit_shout(".$shout->id.", '".$_SERVER['SCRIPT_NAME']."/tfsbox$cp/shouts/edit');", TFsBOXSession::urlRoot()."/images/icons/document-edit.png", NULL, 2);
						TFsBOXAbstractView::renderButton("Delete Shout", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/shouts/delete/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/edit-delete.png", NULL, 2);

						if ($shout->report)
							TFsBOXAbstractView::renderButton("Clear Report", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/reports/clear/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/clear-history.png", NULL, 2);
					}

					else if (!$shout->report
						  && $shout->ranking == TFsBOXSession::REGULAR
						  && $shout->name != TFsBOX::$Session->username())
					{
						$options = true;
						TFsBOXAbstractView::renderButton("Report Shout", $_SERVER['SCRIPT_NAME']."/tfsbox/report/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/operator.png", NULL, 2);
					}
				}

				if (!$options)
					echo "<span class=\"tfsbox_caption\">There are no options available for this shout.</span>\n";

				echo "\t\t</div>\n";
				echo "\t</div>\n";
			}

		function renderShouts($show_header = true, $show_pagination = true)
			{
				if (TFsBOX::$Session->hasErrors())
				{
					echo "<errors>\n";
					foreach (TFsBOX::$Session->errors() as $error)
					{
						echo "<error>$error</error>\n";
					}
					echo "</errors>\n";
				}

				else if (TFsBOX::$Session->hasNotices())
				{
					echo "<notices>\n";
					foreach (TFsBOX::$Session->notices() as $notice)
					{
						echo "<notice>$notice</notice>\n";
					}
					echo "</notices>\n";
				}

				else parent::renderShouts($show_header, $show_pagination);
			}

		function renderShoutForm()
		{
			$this->renderSmilies();

			echo "<form id=\"tfsbox_shoutform\" action=\"". $_SERVER['SCRIPT_NAME'] ."\" method=\"post\" onsubmit=\"tfsbox_add_shout('". $_SERVER['SCRIPT_NAME'].'/tfsbox/shouts' ."'); return false;\">\n";
			echo "<input type=\"text\" name=\"tfsbox_shout\"/>\n";
			echo "<input type=\"submit\" value=\"Shout\"/>\n";
			echo "<input type=\"button\" onclick=\"$('#tfsbox_smilies').toggle('slide', {direction: 'down'}, 150);\" value=\"Smilies\"/>\n";
			if (TFsBOX::$Config->value('auto_refresh', 'yes') == 'yes')
			{
				echo "<input type=\"checkbox\" id=\"tfsbox_refresh_shouts_cb\" checked=\"checked\"/> <label for=\"tfsbox_refresh_shouts_cb\" title=\"Update shouts every ". TFsBOX::$Config->value('antispam_timeout', 5) ." seconds.\">Refresh Shouts</label>\n";
			}

			else
			{
				echo "<input type=\"button\" value=\"Refresh Shouts\" onclick=\"refresh_shouts(TFSBOX_SCRIPT_PATH, true);\"/>\n";
			}
			echo "</form>\n";
		}

		protected function renderSmilies()
			{
				echo "<div id=\"tfsbox_smilies\">";
				foreach (TFsBOX::$Database->smilies() as $key => $value)
				{
					$bbcode = preg_split("#(?<!\\\)\|#", TFsBOX::$Database->smileyBbcodes($key), -1, PREG_SPLIT_NO_EMPTY);
					$bbcode = array_map("stripslashes", $bbcode);
					echo "<img onclick=\"tfsbox_insert_smiley(' ". $bbcode[0] ." ')\" style=\"cursor: pointer;\" src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/$value\" alt=\"$key\"/> ";
				}
				echo "</div>\n";
			}
	}

?>
